import { BrowserModule } from '@angular/platform-browser';
import { NgModule, Injector, APP_INITIALIZER } from '@angular/core';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgZorroAntdModule, NZ_I18N, zh_CN, es_ES, pt_PT, NzI18nService } from 'ng-zorro-antd';
import { registerLocaleData, PlatformLocation } from '@angular/common';
import zh from '@angular/common/locales/zh';
import es from '@angular/common/locales/es';
import pt from '@angular/common/locales/pt';
import { SharedModule } from '@shared/shared.module';
import { LayoutModule } from './layout/layout.module';
import { RoutesModule } from './routes/routes.module';


import { AppSessionService } from '@shared/common/session/app-session.service';
import { AppConsts } from '@shared/AppConsts';
import { AbpModule } from '@abp/abp.module';
import { AbpHttpInterceptor } from '@abp/abpHttpInterceptor';
import { ServiceProxyModule } from '@shared/service-proxy/service-proxy.module';
import { API_BASE_URL } from '@shared/service-proxy/service-proxies';
import { AppPreBootstrap } from './AppPreBootstrap';
import { AppAuthService } from '@shared/common/auth/app-auth.service';
import { UrlHelper } from '@shared/helpers/UrlHelper';
import { AppCommonModule } from '@shared/common/app-common.module';
import { AbpMessage } from '@shared/common/abp-message/abp-message';

// registerLocaleData(es);



export function StartupServiceFactory(injector: Injector, platformLocation: PlatformLocation): Function {
  return () => {


    handleLogoutRequest(injector.get(AppAuthService));

    // 初始化消息类通知
    const abpMessage = injector.get(AbpMessage);
    abpMessage.init();

    return new Promise<boolean>((resolve, reject) => {
      // AppConsts.appBaseHref = getBaseHref(platformLocation);
      // const appBaseUrl = getDocumentOrigin() + AppConsts.appBaseHref;

      AppPreBootstrap.run(() => {
            // 动态设置佐罗的语言
            const nzI18nService: NzI18nService = injector.get(NzI18nService);
            if (abp.localization.currentLanguage.name === 'pu') {
              registerLocaleData(pt);
              nzI18nService.setLocale(pt_PT);
            }
            if (abp.localization.currentLanguage.name === 'zh') {
              registerLocaleData(zh);
              nzI18nService.setLocale(zh_CN);
            }

        const appSessionService: AppSessionService = injector.get(AppSessionService);
        appSessionService.init().then(
          (result) => {
            resolve(result);
          },
          (err) => {
            reject(err);
          }
        );
      });
    });
  };
}




@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    ServiceProxyModule,
    AbpModule,
    AppCommonModule.forRoot(),
    SharedModule,
    LayoutModule,
    RoutesModule
  ],
  providers: [
    { provide: NZ_I18N, useValue: es_ES },

    { provide: HTTP_INTERCEPTORS, useClass: AbpHttpInterceptor, multi: true },
    { provide: API_BASE_URL, useFactory: getServerBaseUrl }, {
      provide: APP_INITIALIZER,
      useFactory: StartupServiceFactory,
      deps: [Injector, PlatformLocation],
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }


export function getServerBaseUrl(): string {
  return AppConsts.serverBaseUrl;
}

function handleLogoutRequest(authService: AppAuthService) {
  const currentUrl = UrlHelper.initialUrl;
  const returnUrl = UrlHelper.getReturnUrl();
  if (currentUrl.indexOf(('account/logout')) >= 0 && returnUrl) {
    authService.logout(true, returnUrl);
  }
}

